# The QtGui.QFileDialog is a dialog that allows users to select files or directories.
# The files can be selected for both opening and saving.
# *** We will also show how a file is opened in PySide. This maybe important for the project

import sys
from PySide import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()

        # Create a Text Edit box
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()

        # Action object for open file. We know action objects are added to menu items
        openFile = QtGui.QAction(QtGui.QIcon("open.jpg"), "Open", self)
        openFile.setShortcut("Ctrl+O")
        openFile.setStatusTip("Open a file!!")
        openFile.triggered.connect(self.showDialog)

        # Add a menu bar to the main window and then populate it with action objects
        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu("&File")
        fileMenu.addAction(openFile)

    def showDialog(self):
        # We pop up the QtGui.QFileDialog. The first string in the getOpenFileName() method is the caption.
        # The second string specifies the dialog working directory. The method returns the selected file name and a
        # filter. We are only interested in the file name.
        # getOpenFileName(parent, [caption, [directory, [filter, [selectedFilter, [options]]]]])
        fname, _ = QtGui.QFileDialog.getOpenFileName(self, "Open a file!!", "D:\My_Documents")
        f = open(fname, 'r')
        with f:
            data = f.read()
            self.textEdit.setText(data)


        self.setGeometry(200, 200, 200, 100)
        self.setWindowTitle("File Dialog")

    def showFontDialog(self):
        font, ok = QtGui.QFontDialog.getFont()
        if ok:
            self.lbl.setFont(font)

if __name__ == "__main__":
    appObject = QtGui.QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(appObject.exec_())